﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Collections;
using SAPbobsCOM;
using BoAddonBiz_FreightForwarder.UpLoad;
using System.Xml;
using System.Text.RegularExpressions;
using BoAddonBiz_FreightForwarder.BusinessBase;

namespace BoAddonBiz_FreightForwarder.Sea
{
    [ControlB1FormAttribute(IsNew = true, FormType = B1FormTypes.ado_AVA_SE_Quote)]
    public class B1Form_Quote_Sea : B1Form_Quote_Base
    {
        #region override Table
        protected override string AVA_OQOT { get { return "@AVA_SE_OQOT"; } }
        protected override string AVA_QOT1 { get { return "@AVA_SE_QOT1"; } }
        protected override string AVA_QOT2 { get { return "@AVA_SE_QOT2"; } }
        protected override string AVA_QOT3 { get { return "@AVA_SE_QOT3"; } }
        protected override string AVA_QOT4 { get { return "@AVA_SE_QOT4"; } }
        protected override string AVA_QOT5 { get { return "@AVA_SE_QOT5"; } }
        protected override string AVA_QOT6 { get { return "@AVA_SE_QOT6"; } }
        protected override string AVA_QOT7 { get { return "@AVA_SE_QOT7"; } }
        private string AVA_QOT8 { get { return "@AVA_SE_QOT8"; } }
        protected override string Target_Delegate_Object { get { return "AVA_SE_Delegate"; } }
        protected override string Target_SalesOrder_Object { get { return "AVA_SE_SalesOrder"; } }
        #endregion
        #region Constructor
        public B1Form_Quote_Sea()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Quote_Sea_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Quote_Sea_frm_ChooseFormListEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_Quote_Sea_frm_FormDataEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_Quote_Sea_frm_MenuEvent);
        }
        #endregion
        #region Events
        void B1Form_Quote_Sea_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.MenuUID == "1292")//添加行
                {
                    if (this.CurrentForm.Settings.MatrixUID == "mtxService")
                    {
                        Matrix mtx = this.CurrentForm.Items.Item(this.CurrentForm.Settings.MatrixUID).Specific;
                        SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, this.CurrentForm.Settings.MatrixUID, "col_f", mtx.Columns.Item("col_f").DataBind.TableName, -1, "LineId", "VisOrder", true);
                    }
                }
                else if (pVal.MenuUID == "1293")//删除行
                {
                    if (this.CurrentForm.Settings.MatrixUID == "mtxService")
                    {
                        Matrix mtx = this.CurrentForm.Items.Item(this.CurrentForm.Settings.MatrixUID).Specific;
                        if (mtx.GetCellFocus() != null)
                        {
                            SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, mtx.Columns.Item("col_f").DataBind.TableName, "LineId", this.CurrentForm.Settings.MatrixUID, "col_f", mtx.GetCellFocus().rowIndex, true);
                        }
                    }
                }
            }
            else
            {

            }
        }

        void B1Form_Quote_Sea_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE)
                {
                    (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).LoadFromDataSource();
                }
            }
            else
            {

            }
        }

        void B1Form_Quote_Sea_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = default(DBDataSource);
            Matrix mtx = default(Matrix);
            if (pVal.Before_Action)
            {
                #region ChooseFormList显示的条件
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);

                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_AirLine_Fee"://委托单位
                    case "CFL_AirLine_Low":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup4";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup6";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }
                #endregion
            }
            else
            {
                if (pVal.ChooseFromListUID == "CFL_AirLine_Fee")
                {

                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT1);
                    mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                    dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                    mtx.GetLineData(pVal.Row);
                    dbds.SetValue("U_CarrierCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    dbds.SetValue("U_CarrierName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    mtx.SetLineData(pVal.Row);
                }
                /*底价信息  航空公司信息*/
                else if (pVal.ChooseFromListUID == "CFL_AirLine_Low")
                {
                    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT5);
                    mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                    dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                    mtx.GetLineData(pVal.Row);
                    dbds.SetValue("U_CarrierCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    dbds.SetValue("U_CarrierName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    mtx.SetLineData(pVal.Row);
                }
            }
        }

        void B1Form_Quote_Sea_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (this.CurrentForm.Mode != BoFormMode.fm_OK_MODE)
                    {
                        (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).LoadFromDataSource();
                    }
                }
            }
            else
            {
                #region 事件后
                #region Switch Panel
                if (pVal.ItemUID == "tabService" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    switch (this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_ContainerType", 0).Trim())
                    {
                        case "FCL":
                        case "1":
                            CurrentForm.PaneLevel = 21;
                            break;
                        case "LCL":
                        case "2":
                            CurrentForm.PaneLevel = 22;
                            break;
                        case "BREAKBULK":
                        case "3":
                            CurrentForm.PaneLevel = 23;
                            break;
                        default:
                            CurrentForm.PaneLevel = 21;
                            break;
                    }
                }
                if (pVal.ItemUID == "FCL" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 21;
                    if (this.CurrentForm.Mode == BoFormMode.fm_ADD_MODE || this.CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE)
                    {
                        Matrix mtx = this.CurrentForm.Items.Item("mtxService").Specific;
                        if (mtx.VisualRowCount == 0)
                        {
                            SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtxService", "col_f", AVA_QOT8, -1, "Lineid", "VisOrder", true);
                        }
                    }
                }
                if (pVal.ItemUID == "LCL" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 22;
                }
                if (pVal.ItemUID == "BULK" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentForm.PaneLevel = 23;
                }
                #endregion
                #region 海运特有，改变单件重量的时候同时计算计费重（单件重量好像还没有单位，不知道是不是可以默认kg）
                if (pVal.EventType == BoEventTypes.et_COMBO_SELECT && new string[] { "cargo_30", "cargo_46" }.Contains<string>(pVal.ItemUID))
                {
                    Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_QOT3, CargoCurrentOffset);
                }
                #endregion
                #region 根据计费吨标准自动计算计费重量
                if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT).GetValue("U_Status", 0).Trim() == "P")
                {
                    if (pVal.ItemUID == "LCL" && pVal.EventType == BoEventTypes.et_CLICK)
                    {
                        ComboBox cb = this.CurrentForm.Items.Item("T_Standard").Specific as ComboBox;
                        string standard = "1000KGS";
                        if (!String.IsNullOrEmpty(cb.Value))
                        {
                            standard = cb.Value;
                        }
                        Utils.CalcBillingWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3), this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT8), standard);
                    }
                    if (pVal.ItemUID == "T_Standard" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                    {
                        ComboBox cb = this.CurrentForm.Items.Item("T_Standard").Specific as ComboBox;
                        string standard = "1000KGS";
                        if (!String.IsNullOrEmpty(cb.Value))
                        {
                            standard = cb.Value;
                        }
                        Utils.CalcBillingWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3), this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT8), standard);
                    }
                }
                #endregion
                #region 修改计费吨重时同步修改货物表的计费重量
                if (pVal.ItemUID == "T_Weight" && pVal.ItemChanged)
                {
                    Utils.SetCargoGrossWeight(this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3), SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString());
                }
                #endregion
                if (pVal.ItemUID == "Chk_In" && (pVal.EventType == BoEventTypes.et_ITEM_PRESSED || pVal.ItemChanged))
                {
                    this.CurrentForm.Items.Item("T_Company").Enabled = false;
                    if (SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, pVal.ItemUID).ToString().Trim() == "Y")
                    {
                        this.CurrentForm.Items.Item("T_Company").Enabled = true;
                    }
                }
                if (pVal.ItemUID == "mtxService" && pVal.ColUID == "col_2" && pVal.ItemChanged)
                {
                    this.CurrentForm.Freeze(true);
                    (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).FlushToDataSource();
                    //不知道为什么MatrixSetting不管用了，所以手动加了下面的代码
                    //double quantity = 0.0d;
                    //double.TryParse(this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT8).GetValue("U_Quantity", pVal.Row - 1), out quantity);
                    //if (quantity > 0.0d && pVal.Row == (this.CurrentForm.Items.Item("mtxService").Specific as Matrix).VisualRowCount)
                    //{
                    //    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtxService", "col_f", AVA_QOT8, -1, "Lineid", "VisOrder", true);
                    //}
                    this.CurrentForm.Freeze(false);
                }
                #endregion
            }
        }
        #endregion
        #region override
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            base.FormEditModeChange(frmEditMode);

            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    //SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(CurrentForm, "mtxService", "col_f", AVA_QOT8, -1, "LineId", "VisOrder", true);
                    break;
            }
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_SE_Quote;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_SE_Quote.ToString() + "Form.srf";

            base.FormInfoSetting(FormSetting);

            #region Matrix
            //配置添加子表的行
            FormSetting.MatrixSetting.MtxUID = "mtxService";
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoAddNewLine = true;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_2";//箱量
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = false;
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = false;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = AVA_QOT8;
            FormSetting.MatrixSetting.Add();
            #endregion
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            base.InitializeForm(InitPar);
            Utils.InitialMatrixComboBox(this.SBOCompany, this.CurrentForm.Items.Item("mtxService").Specific, "col_1", "Code", "Name", "@AVA_FF_OBOX", "Code", null, null, true, true);
        }
        #endregion
        #region methods
        #endregion


    }
}
